home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / devel / make / icmake-6.000 / icmake-6 / icmake / examples / ds < prev    next >
Encoding:
Text File  |  1994-02-08  |  3.9 KB  |  147 lines

  1. #! /usr/local/bin/icmake -qi
  2.  
  3. /*
  4.                                 D S
  5. */
  6.  
  7. #define VERSION     "1.03"
  8. #define YEAR        "1994"
  9.  
  10. int
  11.     debug;
  12. string
  13.     progname,
  14.     xdev;
  15.  
  16. void kill(string s)
  17. {
  18.     printf(s, "\n\n");
  19.     exit(1);
  20. }
  21.  
  22. string backslash_wild(string spec)
  23. {
  24.     string
  25.         s,
  26.         ret;
  27.     int
  28.         index;
  29.  
  30.     for (index = 0; s = element(index, spec); index++)
  31.     {
  32.         if (s == "*" || s == "?")           // wildcard specifiers ?
  33.             ret += "\\";                    // protect the wildcard spec.
  34.         ret += s;
  35.     }
  36.     return (ret);                           // return the protected string
  37. }
  38.  
  39. void preamble(list argv)
  40. {                                         
  41.     progname = get_base(element(0, argv));  // determine progname without .bim
  42.     xdev = "-xdev";                         // no X-device find
  43.     echo(OFF);                              // no display of the exec-ed cmnd
  44. }
  45.  
  46. void option(string arg)
  47. {
  48.     int
  49.         index;
  50.     string
  51.         optchar;
  52.                                             // process all option characters
  53.     for (index = 1; optchar = element(index, arg); index++)
  54.     {
  55.         if (optchar == "x")                 // X-dev ok ?
  56.             xdev == "";                     // set appropriate flag
  57.         else if (optchar == "d")            // debug request
  58.             debug++;                        // set flag
  59.         else                                // kill DS if optchar not found
  60.             kill("Unrecognized option: '-" + optchar);
  61.     }
  62. }
  63.  
  64. list options(int argc, list argv)
  65. {
  66.     int
  67.         index;
  68.     list
  69.         new;
  70.     string
  71.         arg;
  72.  
  73.     for (index = 0; index < argc; index++)  // walk all cmd line arguments
  74.     {
  75.         arg = element(index, argv);         // get next element
  76.         if (element(0, arg) == "-")         // found an option ?
  77.             option(arg);                    // then process it
  78.         else
  79.             new += (list)arg;               // else add to the returnlist
  80.     }
  81.     return (new);                           // return argv-list without options
  82. }
  83.  
  84. void usage()
  85. {
  86.     printf
  87.     (
  88.         "\n"
  89.         "ICCE DS (Disk Search). Version " VERSION "\n"
  90.         "Copyright (c) ICCE " YEAR ". All Rights Reserved\n"
  91.         "\n"
  92.         "DS by Frank B. Brokken\n"
  93.         "\n"
  94.         "Usage: ", progname, " [options] [dir-spec] file\n"
  95.         "Where:\n"
  96.         "   options:\n"
  97.         "       -d: Display rather than execute the search command\n"
  98.         "       -x: Allow cross-device searches\n"
  99.         "   dir-spec:   specification of the directory where the search is to\n"
  100.         "               be started. By default: / (the root).\n"
  101.         "   file: name of file to search.\n"
  102.         "\n"
  103.         "For the 'file' argument quoted wildcards (e.g., ds '*.local') are ok.\n"
  104.         "\n"
  105.     );
  106.     exit (1);
  107. }
  108.  
  109. void process(int argc, list argv)
  110. {
  111.     string
  112.         cmd,
  113.         filespec,
  114.         startdir;
  115.  
  116.     if (argc == 2)                          // a file given as argument
  117.         startdir = "/";                // start at the root
  118.     else
  119.         startdir = element(1, argv);        // otherwise start at specified dir
  120.  
  121.                                             // protect wildcards in the
  122.                                             // filespecification with \-char
  123.     filespec = backslash_wild(element(argc - 1, argv));
  124.     
  125.     cmd = "find " + startdir + " " + xdev + " -name " + filespec + 
  126.                                             " 2>/dev/null"; 
  127.  
  128.     if (!debug)
  129.         system(P_NOCHECK, cmd);
  130.     else
  131.         printf(cmd, "\n");
  132. }
  133.  
  134. int main(int argc, list argv)
  135. {
  136.     preamble(argv);                         // preamble: determine progname etc.
  137.     argv = options(argc, argv);             // process options
  138.     argc = sizeof(argv);
  139.  
  140.     if (argc == 1)                          // no arguments ?
  141.         usage();                            // give help
  142.     
  143.     process(argc, argv);                    // else process arguments
  144.     
  145.     return (0);
  146. }
  147.